<?php

namespace Handler;

use Workerman\Lib\Timer;
use Model\RepData;

class WMHandler {
	/**
	 * 单例
	 *
	 * @var WMHandler
	 */
	private static $m_Instance;
	/**
	 * 大厅后台地址
	 * 
	 * @var string
	 */
	private $m_Url = "http://127.0.0.1:1007/";
	/**
	 *
	 * @var integer
	 */
	private $m_GameId = 1;
	/**
	 * 游戏名
	 *
	 * @var string
	 */
	private $m_GameName = '游戏名';
	/**
	 * 构造函数
	 */
	protected function __construct() {
		Timer::add ( 1, array (
				$this,
				'mb_TimerTask' 
		), array (), true );
	}
	/**
	 * 析构函数
	 */
	protected function __destruct() {
	}
	/**
	 * 获取单例
	 *
	 * @return \Handler\WMHandler
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	/**
	 * 时间任务(每一秒执行一次)
	 */
	public function mb_TimerTask() {
	}
	/**
	 * 初始化
	 *
	 * @param unknown $url        	
	 * @param unknown $gameId        	
	 * @param unknown $gameName        	
	 */
	public function mb_Init($url, $gameId, $gameName) {
		$this->m_Url = $url;
		$this->m_GameId = $gameId;
		$this->m_GameName = $gameName;
	}
	/**
	 * 登录
	 *
	 * @param string $name        	
	 * @return \Model\RepData
	 */
	public function mb_Login($name) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_GameLogin',
					'name' => $name,
					'gameid' => $this->m_GameId 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 登出
	 *
	 * @param string $name        	
	 * @return \Model\RepData
	 */
	public function mb_Logout($name) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_GameLogout',
					'name' => $name,
					'gameid' => $this->m_GameId 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 *
	 * @param unknown $name        	
	 * @return \Model\RepData
	 */
	public function mb_Query($name) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_GameQuery',
					'name' => $name 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 *
	 * @param unknown $name        	
	 * @param unknown $coin        	
	 * @param unknown $ticket        	
	 * @param unknown $remark        	
	 * @return \Model\RepData
	 */
	public function mb_Save($name, $coin, $ticket, $remark) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_GameSave',
					'name' => $name,
					'coin' => $coin,
					'ticket' => $ticket,
					'remark' => $remark,
					'gameid' => $this->m_GameId 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 进入房间
	 * 
	 * @param unknown $num        	
	 * @param unknown $name        	
	 * @return \Model\RepData
	 */
	public function mb_EnterRoom($num, $name) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_EnterRoom',
					'num' => $num,
					'name' => $name 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 退出房间
	 * 
	 * @param unknown $name        	
	 * @return \Model\RepData
	 */
	public function mb_ExitRoom($name) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_ExitRoom',
					'name' => $name 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 房间游戏开始
	 * 
	 * @param unknown $num        	
	 * @return \Model\RepData
	 */
	public function mb_RoomGameStart($num) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_RoomGameStart',
					'num' => $num 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 房间游戏结束
	 * 
	 * @param unknown $num        	
	 * @return \Model\RepData
	 */
	public function mb_RoomGameOver($num) {
		for($i = 0; $i < 5; $i ++) {
			$pData = array (
					'zone' => 'Web',
					'method' => 'mb_RoomGameOver',
					'num' => $num 
			);
			$repData = $this->mb_DecodeWMData ( $this->mb_SendPost ( $this->m_Url, $pData ) );
			if ($repData->m_Result != ErrCode_Network)
				return $repData;
		}
		return $repData;
	}
	/**
	 * 发送POST数据
	 *
	 * @param unknown $url        	
	 * @param unknown $post_data        	
	 * @return string
	 */
	private function mb_SendPost($url, $pData) {
		$postData = json_encode ( $pData );
		$ch = curl_init ( $url );
		curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postData );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
				'Content-Type: application/json',
				'Content-Length: ' . strlen ( $postData ) 
		) );
		$data = curl_exec ( $ch );
		return $data;
	}
	/**
	 *
	 * @param unknown $jData        	
	 * @return \Model\RepData
	 */
	private function mb_DecodeWMData($jData) {
		if ($jData == '') {
			$repData = new RepData ();
			$repData->m_Result = ErrCode_Network;
			$repData->m_Data = '网络连接错误';
			$repData->m_Sign = '';
			return $repData;
		} else {
			return json_decode ( $jData );
		}
	}
}